package com.qfedu.lc.user.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qfedu.lc.common.alipay.AliPayUtil;
import com.qfedu.lc.common.param.PayAdd;
import com.qfedu.lc.common.vo.R;
import com.qfedu.lc.user.entity.MoneyLog;
import com.qfedu.lc.user.entity.PayLog;
import com.qfedu.lc.user.entity.Wallet;
import com.qfedu.lc.user.mapper.MoneyLogMapper;
import com.qfedu.lc.user.mapper.PayLogMapper;
import com.qfedu.lc.user.mapper.WalletMapper;
import com.qfedu.lc.user.service.WalletService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 6.用户钱包表 服务实现类
 * </p>
 *
 * @author wgz
 * @since 2024-01-29
 */
@Service
public class WalletServiceImpl extends ServiceImpl<WalletMapper, Wallet> implements WalletService {

    @Autowired
    private WalletMapper walletMapper;

    @Autowired
    private MoneyLogMapper moneyLogMapper;

    @Autowired
    private PayLogMapper payLogMapper;

    @Override
    public boolean subWallet(int userId, int money) {

        // 1 查询该账户 是否为空
        QueryWrapper<Wallet> walletQueryWrapper = new QueryWrapper<>();
        walletQueryWrapper.eq("user_id",userId);
        Wallet wallet = walletMapper.selectOne(walletQueryWrapper);

        if (wallet==null){ // 该用户没有账户余额
            return false;
        }

        // 2 账户余额是否充裕
        if (wallet.getMoney()< money*100){
            return false;
        }

        // 3.削减账户余额
        wallet.setMoney(wallet.getMoney() - money*100);
        int num = walletMapper.updateById(wallet);


        // 4.记录账户余额日志
        // type 1增加 2减少
        MoneyLog moneyLog = new MoneyLog(userId,money*100,2,"vip购买","购买vip会员花费"+money + "元",new Date());
        moneyLogMapper.insert(moneyLog);

        if (num > 0) {
             return  true;
        }

        return false;
    }

    @Override
    public R rechargeByZhiFuBao(PayAdd payAdd) {
        // 1.记录日志

        String title  = "支付宝充值"+payAdd.getMoney();

        String no = "w" + System.currentTimeMillis() + RandomUtil.randomNumbers(6);
        int money = (int) (payAdd.getMoney()* 100);  //以分为单位

        PayLog payLog = new PayLog(payAdd.getUserId(),payAdd.getPayType(),money,title,no,0,1,new Date(),new Date());
        int num = payLogMapper.insert(payLog);

        if (num>0){
           String html =    AliPayUtil.createPay(no,payAdd.getMoney(),title);
           return R.ok(html);
        }


        return R.fail("调用支付宝包充值失败 ");
    }

    @Override
    public R rechargeByZhiFuBaoCallBack(String no) {

        // 1.修改支付订单
        QueryWrapper<PayLog> payLogQueryWrapper = new QueryWrapper<>();
        payLogQueryWrapper.eq("no",no);
        PayLog payLog = payLogMapper.selectOne(payLogQueryWrapper);

        if (payLog==null){
            return R.fail("充值失败");
        }

        payLog.setStatus(2);

        int num = payLogMapper.updateById(payLog);
        if (num <= 0){
            return R.fail("充值失败");
        }

        // 2.增加账户余额
        // 账户 可能为null
        QueryWrapper<Wallet> walletQueryWrapper = new QueryWrapper<>();
        walletQueryWrapper.eq("user_id",payLog.getUserId());
        Wallet wallet = walletMapper.selectOne(walletQueryWrapper);

        if (wallet==null){ // 该账户不存在，直接插入即可

            wallet = new Wallet();
            wallet.setMoney(payLog.getMoney())
            .setTotalMoney(payLog.getMoney())
            .setUserId(payLog.getUserId())
            .setUtime(new Date())
            .setCtime(new Date());

            num = walletMapper.insert(wallet);
        }else {     // 直接增加余额

            wallet.setMoney(wallet.getMoney() + payLog.getMoney())
                    .setTotalMoney(wallet.getTotalMoney()+payLog.getMoney())
                    .setUtime(new Date());

            num = walletMapper.updateById(wallet);
        }


        // 3.记录账户变动日志
        // type 1增加 2减少
        MoneyLog moneyLog = new MoneyLog(payLog.getUserId(),payLog.getMoney(),1,"账户充值","账户充值"+payLog.getMoney()/100 + "元",new Date());
        moneyLogMapper.insert(moneyLog);

        if (num>0){
            return R.ok("充值成功");
        }

        return R.fail("充值失败");
    }
}
